//logs.js
const app = getApp();

Page({
  data: {
    brand: "",
    model: "",
    screen: "",    
    system: "",
    pixelRatio: "",
    net: "",
    icon: "wifi-off",
    ip_address: "正在解析中...",
    ISP: "正在解析中...",
  },

  onLoad: function (opts) {
    var that = this;    
    wx.getNetworkType({                           //获取当前网络状态    
      success: function (res) {
        if (res.networkType == '4g') {          
          var net = "4g"
          var iconurl = "wifi-off"
        }
        if (res.networkType == '3g') {          
          var net = "3g"
          var iconurl = "wifi-off"
        }
        if (res.networkType == '2g') {          
          var net = "2g"
          var iconurl = "wifi-off"
        }
        if (res.networkType == 'wifi') {          
          var net = "wifi"
          var iconurl = "wifi-on"
        }
        if (res.networkType == 'none') {          
          var net = "当前无网络"
          var iconurl = "wifi-off"
        }         
        that.setData({
          net: net,
          icon: iconurl
        })
      }
    })
    wx.onNetworkStatusChange(function (res) {     //监听当前网络状态
      if (res.networkType == '4g') {       
        var net = "4g"
        var iconurl = "wifi-off"
      }
      if (res.networkType == '3g') {        
        var net = "3g"
        var iconurl = "wifi-off"
      }
      if (res.networkType == '2g') {        
        var net = "2g"
        var iconurl = "wifi-off"
      }
      if (res.networkType == 'wifi') {        
        var net = "wifi"
        var iconurl = "wifi-on"
      }
      if (res.networkType == 'none') {        
        var net = "当前无网络"
        var iconurl = "wifi-off"
      }  
      that.setData({
        net: net,
        icon: iconurl
      })
      that.getIpAddress();
    }),    
    wx.getSystemInfo({      
      success: function (res) {       
        that.setData({
          brand: res.brand,          
          model: res.model,
          screen: res.screenWidth + " * " + res.screenHeight,           
          system: res.system,
          pixelRatio: res.pixelRatio,          
        })
      }
    }),
    this.getIpAddress()    
  },
  getIpAddress: function () {
    var that = this
    wx.request({
      url: app.globalData.url + '/ip',  //http://ip-api.com/json
      success: function (e) {
        console.log(e.data)
        var isp = e.data.as
        var isp_cn = ""
        if (isp.indexOf("Telecom") != -1 || isp.indexOf("Technology") != -1) {
          isp_cn = "中国电信"
        }
        else if (isp.indexOf("Unicom") != -1) {
          isp_cn = "中国联通"
        }
        else if (isp.indexOf("Mobile") != -1) {
          isp_cn = "中国移动"
        } 
        else if (isp.indexOf("Netcom") != -1) {
          isp_cn = "中国网通"
        }
        else if (isp.indexOf("Railway") != -1) {
          isp_cn = "中国铁通"
        }
        else{
          isp_cn = "未知运营商"
        }
        that.setData({
          ip_address: e.data.query,   //IP地址          
          ISP: isp_cn
        })
      }
    })  
  },
  onShareAppMessage: function () { //用户点击右上角分享   
    return {
      title: '本机信息分享', // 分享标题
      desc: '本机信息分享', // 分享描述
      path: 'pages/system/system' // 分享路径
    }
  },  
})